package com.bucks.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**

 * @date 2019-11-15
 */
@Slf4j
@Component
public class PerformanceInterceptor implements HandlerInterceptor {
    private ThreadLocal<StopWatch> stopWatchThreadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        StopWatch stopWatch = new StopWatch();
        stopWatchThreadLocal.set(stopWatch);
        stopWatch.start();
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        stopWatchThreadLocal.get().stop();
        stopWatchThreadLocal.get().start();
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        StopWatch stopWatch = stopWatchThreadLocal.get();
        stopWatch.stop();
        String method = handler.getClass().getSimpleName();
        if (handler instanceof HandlerMethod){
            String beanType = ((HandlerMethod) handler).getBeanType().getName();
            String methodName = ((HandlerMethod) handler).getMethod().getName();
            method = beanType+"."+methodName;
        }
        log.info("{};{};{};{};{}ms;{}ms;{}ms;" ,request.getRequestURI() ,method ,
                response.getStatus(),ex == null ? "-" : ex.getClass().getSimpleName(),
                stopWatch.getTotalTimeMillis(),stopWatch.getTotalTimeMillis() - stopWatch.getLastTaskTimeMillis(),
                stopWatch.getLastTaskTimeMillis());
        stopWatchThreadLocal.remove();
    }
}
